package game.managers
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	//import xygame.pub.PositionDesc;
	
	/**
	 * 舞台管理类
	 * @author xiezhicheng
	 */
	public class StageManager extends EventDispatcher
	{
		private static var _instance:StageManager;
		private var _width:Number = 0;
		private var _height:Number = 0;
		/** 舞台 */
		public var stage:Stage;
		
		public var oldWidth:Number = 950;
		public var oldHeight:Number = 600;
		
		public static const MAX_HEIGHT:Number = 600;
		
		/** 舞台的宽度改变 */
		public static const CHANGE_STAGE_WIDTH:String = "change_stage_width";
		/** 舞台的高度改变 */
		public static const CHANGE_STAGE_HEIGHT:String = "change_stage_height";
		/** 改变舞台尺寸 */
		public static const CHANGE_STAGE_SIZE:String = "change_stage_size";
		
		public function StageManager()
		{
			if(_instance == null){
				super();
				_instance = this;
			}else{
				throw new Error("StageManager is single instance...");
			}
		}
		
		public static function getInstance():StageManager
		{
			if(!_instance)
				_instance = new StageManager();
			return _instance;
		}
		
		/**
		 * 初始化舞台管理器
		 */
		public function init(_stage:Stage):void
		{
			stage = _stage;
			oldWidth = stage.stageWidth;
			oldHeight = stage.stageHeight;
			_dic = new Dictionary();
			stage.addEventListener(Event.RESIZE,stageResizeHandler);
		}

		
		/** 舞台宽度 */
		public function get width():Number
		{
			return _width;
		}
		
		
		/** 舞台高度 */
		public function get height():Number
		{
			return _height;
		}
		
		
		private function stageResizeHandler(evt:Event):void
		{
			if(_width > 0)
			{
				oldWidth = _width;
				oldHeight = _height;
			}
			
			_width = stage.stageWidth;
			_height = stage.stageHeight;
			
			if(_width != StageProxy.width){
				var offx:Number =stage.stageWidth - StageProxy.width;
				StageProxy.width = _width;
				
//				var event1:GameEvent = new GameEvent(CHANGE_STAGE_WIDTH);
//				event1.param = offx;
//				dispatchEvent(event1);
			}
			
			if(_height != StageProxy.height){
				var offy:Number = _height - StageProxy.height;
				//超过768，在缩小处理
				if(StageProxy.height >= MAX_HEIGHT)
					offy = offy + (StageProxy.height - MAX_HEIGHT);
				StageProxy.height = _height;
				
//				var event2:GameEvent = new GameEvent(CHANGE_STAGE_HEIGHT);
//				event2.param = offy;
//				dispatchEvent(event2);
			}
			
			//更新注册的对象的位置
			adaptive();
			
			//发布改变屏幕大小事件
//			var event:GameEvent = new GameEvent(CHANGE_STAGE_SIZE);
//			dispatchEvent(event);
		}
		
		
		private var _dic:Dictionary;
		
		/**
		 * 将对象添加到自适应列表中
		 * @param obj:DisplayObject 要添加的对象
		 * @param pos:String 显示位置"center" "left" "right" "top" "bottom" "common" ...见【PositionDesc】
		 */
		public function addToAdaptiveList(obj:DisplayObject,pos:String = "common"):void
		{
			var des:Object = _dic[obj];
			
			if(des == null)
			{
				des = new Object();
				_dic[obj] = des;
			}
			
			des.x = obj.x;
			des.y = obj.y;
			des.pos = pos;
		}
		
		
		/**
		 * 自适应，更新位置
		 */
		public function adaptive():void
		{
			var ratx:Number = _width / oldWidth;
			var raty:Number = _height / oldHeight;
			
			var offx:Number = _width - oldWidth;
			var offy:Number = _height - oldHeight;
			
			for(var s:* in _dic)
			{
				var obj:DisplayObject = s as DisplayObject;
				var des:Object = _dic[s];
				//trace("初始位置：" + des.x + " : " + des.y);
				
			/*	if(des.pos == PositionDesc.COMMON)
				{
					des.x *= ratx;
					des.y *= raty;
				}
				else if(des.pos == PositionDesc.CENTER)
				{
					des.x = width - obj.width >> 1;
					des.y = height - obj.height >> 1;
				}
				else if(des.pos == PositionDesc.LEFT_TOP)
				{
					//维持原有位置
				}
				else if(des.pos == PositionDesc.LEFT_BOTTOM)
				{
					des.y += offy;
				}
				else if(des.pos == PositionDesc.LEFT_CENTER)
				{
					des.y = height - obj.height >> 1;
				}
				else if(des.pos == PositionDesc.RIGHT_TOP)
				{
					des.x += offx;
				}
				else if(des.pos == PositionDesc.RIGHT_BOTTOM)
				{
					des.x += offx;
					des.y += offy;
				}
				else if(des.pos == PositionDesc.RIGHT_CENTER)
				{
					des.x += offx;
					des.y = height - obj.height >> 1;
				}
				else if(des.pos == PositionDesc.BOTTOM_CENTER)
				{
					des.y += offy;
					des.x = width - obj.width >> 1;
				}
				else if(des.pos == PositionDesc.TOP_CENTER)
				{
					des.x = width - obj.width >> 1;
				}
				//trace("===========> " + des.x + " : " + des.y);
				
				if(obj)
				{
					obj.x = des.x;
					obj.y = des.y;
				}*/
			}
		}
		
		/**
		 * 使对象处于父容器的中心
		 */
		public function center(obj:DisplayObject):void
		{
			if(obj.parent)
			{
				obj.x = width - obj.width >> 1;
				obj.y = height - obj.height >> 1;
			}
		}
		
		override public function toString():String
		{
			var str:String = super.toString();
			str += " >> " + stage.stageWidth + " : " + stage.stageHeight;
			return str;
		}
	}
}
